﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Diagnostics;
using System.Threading;
namespace OtherDevice
{
    [Description("串口")]
    public partial class Frm_SerialPort : Form_ControlBase
    {
        COM_Base com = null;
        public Frm_SerialPort()
        {
            InitializeComponent();
        }
        public override void FlushToDlg(object devicebase)
        {
            try
            {
                if (devicebase == null)
                    return;
                this.com = devicebase as COM_Base;
                this.Dock = DockStyle.Fill;
                this.Show();
            }
            catch (Exception)
            {

            }

        }
        private void uiButton1_Click(object sender, EventArgs e)
        {
            try
            {
                string str = txt_发送数据.Text;
                com.m_SerialPort.Write(str);
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                while (stopwatch.ElapsedMilliseconds < 6000)
                {
                    Thread.Sleep(100);
                    str = com.m_SerialPort.ReadExisting();
                    if (str != "")
                    {
                        list_Msg.Items.Add(str);
                        return;
                    }
                }
                UIMessageBox.ShowError("读取数据超时");
            }
            catch (Exception)
            {
                return ;
            }

        }

        private void 清除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            list_Msg.Items.Clear();
        }
    }
}
